import { baseUrl } from './config.js';
import Toast from '../dist/toast/toast';

/**
 * url：请求地址
 * data：请求参数
 * loading：可选项，是否需要loading，默认为false
 */

// GET请求
const get = (url, data, loading) => {
    let token = wx.getStorageSync("token");
    // console.log(token);
    url = baseUrl + url;
    return new Promise((resolve, reject) => {
        // loading
        if (loading) {
            wx.showLoading({
                title: "玩命加载中..."
            })
        }
        // 请求
        wx.request({
            url,
            methods: "get",
            data,
            header: {
                'Authorization': 'Bearer ' + token
            },
            success: (res) => {
                if (res.data.code === 666) {
                    resolve(res.data);
                } else if (res.data.msg === "token已过期") {
                    wx.removeStorageSync('token')
                    wx.showModal({
                        title: '提示',
                        content: '登录已过期，是否重新登录',
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: "/pages/my/my"
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                } else {
                    console.log(res.data.msg);
                }
            },
            fail: (err) => {
                Toast.fail('网络异常');
                console.log("请求失败信息", err);
            },
            complete: () => {
                wx.hideLoading();
            }
        })
    })
}

// POST请求
const post = (url, data, loading) => {
    let token = wx.getStorageSync("token");
    // console.log(token);
    url = baseUrl + url;
    return new Promise((resolve, reject) => {
        if (loading) {
            wx.showLoading({
                title: '玩命加载中。。。。',
            })
        }
        wx.request({
            url,
            method: 'post',
            data,
            header: {
                'Authorization': 'Bearer ' + token
            },
            success: (res) => {
                if (res.data.code === 666) {
                    resolve(res.data);
                } else if (res.data.msg === "token已过期") {
                    wx.removeStorageSync('token')
                    wx.showModal({
                        title: '提示',
                        content: '登录已过期，是否重新登录',
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: "/pages/my/my"
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                } else {
                    console.log(res.data.msg);
                }
            }, fail: (err) => {
                Toast.fail('网络异常');
                console.log('请求失败信息', err);
            }, complete: () => {
                wx.hideLoading();
            }
        })
    })
}

export default { get, post }
